package eu.sudparis.it.opp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import eu.sudparis.it.opp.demo.GraphAnalysis;
import eu.sudparis.it.opp.ex.SocialTags;
import eu.sudparis.it.opp.graph.Edge;

public class Community <T extends Node>{
	public static long cont_index=60*2;
	public TimeSeries attach;
	public List<T> participants = new ArrayList<T>();
	public HashMap<String, List<Integer>> shared_interests = new HashMap<String, List<Integer>>();
	public List<Edge> interactions = new ArrayList<Edge>();
	public long first_observed = -1;
	public long last_observed = -1;
	
	public Community(TimeSeries attach) {
		this.attach = attach;
	}

	public Community(List<T> nodes, TimeSeries attach) {
		this(attach);
		this.addParticipants(nodes);
	}

	public boolean interact(Node id1, Node id2) {
		if (this.interactions.size() == 0)
			this.generateEdges();
		return this.interactions.contains(new Edge(id1.id, id2.id));
	}

	public void generateEdges() {
		for (Node nd : participants) {
			for (Node ndd : nd.inContact) {
				if (this.participants.contains(ndd)) {
					Edge edge = new Edge(nd.id, ndd.id);
					if (!this.interactions.contains(edge))
						this.interactions.add(edge);
				}
			}
		}
	}

	public List<T> getParticipants() {
		return participants;
	}

	public void addParticipants(List<T> participants) {
		this.participants.addAll(participants);
	}

	public HashMap<String, List<Integer>> getShared_interests() {
		return shared_interests;
	}

	public void addShared_interests(String dem, List<Integer> shared_interests) {
		this.shared_interests.put(dem, shared_interests);
	}

	public void addShared_interests(String dem, int shared_interest) {
		if (!this.shared_interests.containsKey(dem))
			this.shared_interests.put(dem, new ArrayList<Integer>());
		this.shared_interests.get(dem).add(shared_interest);
	}
	public String toString(){
		return "Participants:\t"+this.participants
		        +"\nShared Interests:\t"+this.shared_interests
		        +"\nfrom:\t"+GraphAnalysis.toDateSinceInfoCom(this.first_observed)
		        +"\tto:\t"+GraphAnalysis.toDateSinceInfoCom(this.last_observed);
	}
	public boolean equals(Object obj) {
		Community com = (Community) obj;
		if (com.participants.containsAll(this.participants)
				&& this.participants.containsAll(com.participants)) {
			if(this.first_observed>com.first_observed){
				boolean re= (last_observed-com.last_observed)<=cont_index;
				if(re){
					com.last_observed+=cont_index;
				}
				return re;
			}else if(this.first_observed<com.first_observed){
				boolean re= (com.last_observed-this.last_observed)<=cont_index;
				if(re){
					last_observed+=cont_index;
				}
				return re;
			}
			return false;
		}
		return false;
	}
	public SocialTags getTags(){
		SocialTags st=new SocialTags();
		st.profile=this.shared_interests;
		st.status=SocialTags.community;
		return st;
	}
}
